package org.example.netty.handler.channel;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import org.example.netty.model.CustomMessage;

import java.nio.charset.StandardCharsets;

/**
 * @author:hzc
 * @date:2025/5/23
 * @desc:
 */
public class MessagePrintHandler extends SimpleChannelInboundHandler<CustomMessage> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, CustomMessage msg) throws Exception {
        System.out.println("收到消息");
        String s = new String(msg.getContent(), StandardCharsets.UTF_8);
        System.out.println("发送的消息是:"+s);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        System.out.println("抛错误了");
        ctx.close();
        super.exceptionCaught(ctx, cause);
    }
}
